#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
#include <QLineSeries>
#include <QtSerialPort/QSerialPort>         // 提供访问串口的功能
#include <QtSerialPort/QSerialPortInfo>      // 提供系统中存在的串口信息
#include <QtCharts/QChartGlobal>
#include <QtCharts/QChart>
#include <QNetworkAccessManager>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QTcpServer *tcpServer;
    QTcpSocket *tcpSocket;
    QLineSeries *serie1 = new QLineSeries();
    QLineSeries *serie2 = new QLineSeries();
    QLineSeries *serie3 = new QLineSeries();
    QLineSeries *serie4 = new QLineSeries();
    QChart* chart1 = new QChart();
    QChart* chart2 = new QChart();
    QString date[5];
    QString temperature[5];
    QString weather_type[5];
    // QString wid[5];
    // QString direct[5];
    QLabel *lab_temp_range[5];
    QLabel *lab_date[5];
    QLabel *lab_weather_type_pic[5];
    void openDb();
    /*********************************串口基本设置相关函数*********************************/
    void    serialPortInit();      //串口初始化
    void    windowInit();          //显示窗口初始化
    void    refreshCom();          //刷新串口
    /******************************************************************/
    void chartInit();
    /*********************************天气相关函数*********************************/
    void get_Ip(void);
    void json_Ip(QNetworkReply *reply);
    void get_City(QString ip);
    void json_City(QNetworkReply *reply);
    void get_Weather(QString city);
    void json_Weather(QNetworkReply *reply);
    void data_Update();
private slots:
    /*********************************串口基本设置相关函数*********************************/
    void sendData();            //发送串口数据
    void receiveData();         //接收串口数据
    void openSerialport();      //串口开启
    void closeSerialport();     //串口关闭
    void setBuad(int);          //设置波特率
    void comboBox_buadClicked(); //comboBox被点击
    void clearRcv();            //清除接收缓存
    void handleSerialError(QSerialPort::SerialPortError serialPortErr); //串口异常捕获
    void rcvDataOutputToFile();     //串口输出内容输出到文件
    /******************************************************************/
    void on_OpenBt_Clicked();
    void on_CloseBt_Clicked();
    void on_SendBt_Clicked();
    void on_ClearBt_Clicked();
    void newConnection_Slot();
    void readyRead_Slot();
    void disconnected_Slot();
    void on_SearchBt_Clicked();
    void on_LocateBt_Clicked();

private:
    Ui::MainWindow *ui;
    int count;
    QSerialPort* serialPort;
    QSqlDatabase db;
    QString insert_sql = "INSERT INTO monitor VALUES (?, ?, ?, ?, ?)";

    QNetworkAccessManager requst_ip;
    QNetworkAccessManager requst_city;
    QNetworkAccessManager requst_weather;
    QMap<QString,QString> map1;

    QString TEXT_COLOR(QString color,QString str);
};
#endif // MAINWINDOW_H
